<?php
namespace modules\game\services;

class Rule{
	
	public $_pair 			= 1;//扑克牌 副数
	public $_joinPerson 	= [];//参加人
	public $_totalPoker 	= [];//扑克牌
	public $_joinNum 		= 0;//参加人数
	public $_pokerNum 		= 0;//扑克牌数
	public $_singleNum  	= 0;//每人发牌数量
	public $_waitTime   	= 30;//等待时间s,超过该时间,系统出牌
	public $_isKingPoker  	= false;//针对最近上一个出牌人身份，是否地主，用于出牌力度 调度
	//原始一副牌
	public function getRawPoker(){
		return [//红桃1 方块2 黑桃3 花4
			31,32,33,34,
			41,42,43,44,
			51,52,53,54,
			61,62,63,64,
			71,72,73,74,
			81,82,83,84,
			91,92,93,94,
			101,102,103,104,
			111,112,113,114,
			121,122,123,124,
			131,132,133,134,
			141,142,143,144,//A 用14
			161,162,163,164,//2 用16
			171,181,//小王171 大王181
		];
	}
	public function join(Person $person){
		$person->_rule = $this;
		$this->_joinPerson[] = $person;
	}
	public function getTotalPoker(){
		for($i = 0 ; $i < $this->_pair ; ++$i) {
			$this->_totalPoker = array_merge($this->_totalPoker,$this->getRawPoker());
		}
	}
	public function issuePoker(){
		if(!$this->_joinPerson){
			die('no body join , can not issue poker');
		}
		$this->_joinNum = count($this->_joinPerson);
		$this->getTotalPoker();
		$this->_pokerNum = count($this->_totalPoker);
		if($this->_pokerNum % $this->_joinNum){
			die('poker num is not apply for join num');
		}
		$this->_singleNum = $this->_pokerNum / $this->_joinNum;
		shuffle($this->_totalPoker);
		$offset = 0;
		foreach($this->_joinPerson as $person){
			$poker = array_slice($this->_totalPoker, $offset,$this->_singleNum);
			sort($poker);
			$person->_mypoker = $poker;
			$offset += $this->_singleNum;
		}
	}

	// public $_testPoker    = [];//待检验牌
	// public $_testNum      = 0;
	public $_accordMethod = '';//如果出牌符合规则，则符合的规则为
	public $_outPoker     = [];//当前场面的牌,(应压的牌)
	//单张  //单张顺 
	//对子  //对子顺
	//三张      //双飞
	//四张  // 三代一 三飞带多
	//四代二
	//王炸
	const SINGLE_P          = 'SingleP'; 
	const SINGLE_P_STRAIGHT = 'SinglePStraight'; 
	const DOUBLE_P			= 'DoubleP'; 	
	const DOUBLE_P_STRAIGHT	= 'DoublePStraight';
	const THREE_P			= 'ThreeP';
	const THREE_P_STRAIGHT	= 'ThreePStraight';
	const FOUR_P			= 'FourP';
	const THREE_P_BY_ONE    = 'ThreePByOne';
	const FOUR_P_BY_TWO     = 'FourPByTwo'; 
	const TWO_KING          = 'TwoKing';

	const METHOD_NAMESPACE  = 'modules\\game\\services\\method\\';

	//真人斗地主，出牌规则检测即可
	public function ruleTest(Array $poker){
		if(!$poker) return false;
		$methods = [];
		$methods[self::SINGLE_P] 			= self::METHOD_NAMESPACE . self::SINGLE_P;
		$methods[self::SINGLE_P_STRAIGHT] 	= self::METHOD_NAMESPACE . self::SINGLE_P_STRAIGHT;
		$methods[self::DOUBLE_P] 			= self::METHOD_NAMESPACE . self::DOUBLE_P;
		$methods[self::DOUBLE_P_STRAIGHT] 	= self::METHOD_NAMESPACE . self::DOUBLE_P_STRAIGHT;
		$methods[self::THREE_P] 			= self::METHOD_NAMESPACE . self::THREE_P;
		$methods[self::THREE_P_STRAIGHT] 	= self::METHOD_NAMESPACE . self::THREE_P_STRAIGHT;
		$methods[self::FOUR_P] 				= self::METHOD_NAMESPACE . self::FOUR_P;
		$methods[self::THREE_P_BY_ONE] 		= self::METHOD_NAMESPACE . self::THREE_P_BY_ONE;
		$methods[self::FOUR_P_BY_TWO] 		= self::METHOD_NAMESPACE . self::FOUR_P_BY_TWO;
		$methods[self::TWO_KING] 			= self::METHOD_NAMESPACE . self::TWO_KING;
		foreach($methods as $method => $namespace){
			if($namespace::getInstance()->test($poker)){
				$this->_accordMethod = $method;
				return true;
			}
		}
		return false;
	}
}